<template>
  <div class="employees-container">
    <div class="app-container">
      <PageTools :show-before="true">
        <template #before>
          {{ total }}

        </template>
        <template #after>
          <el-button size="small" type="warning" @click="$router.push('/employees/import')">导入</el-button>
          <el-button size="small" type="danger" @click="exportToExcel">导出</el-button>
          <el-button size="small" type="primary" @click="showDialog=true">新增员工</el-button>
        </template>
      </PageTools>
      <el-card>
        <el-table border :data="employeeList">
          <el-table-column label="序号" type="index" sortable="" />
          <el-table-column label="姓名" prop="username" sortable="" />
          <el-table-column label="工号" prop="workNumber" sortable="" />
          <el-table-column label="聘用形式" :formatter="formatEmployment" prop="formOfEmployment" sortable="" />
          <el-table-column label="部门" prop="departmentName" sortable="" />
          <el-table-column label="入职时间" prop="timeOfEntry" sortable="">
            <template v-slot="scope">
              {{ scope.row.timeOfEntry | formatDate }}
            </template>
          </el-table-column>>
          <el-table-column label="账户状态" prop="enableState" sortable="">
            <template v-slot="scope">
              <el-switch
                v-model="scope.row.enableState"
                :active-value="1"
                :inactive-value="2"
                disabled
              />
            </template>
          </el-table-column>
          <el-table-column label="操作" sortable="" fixed="right" width="280">
            <template v-slot="scope">
              <el-button type="text" size="small" @click="$router.push('/employees/detail'+scope.row.id)">查看</el-button>
              <el-button type="text" size="small">转正</el-button>
              <el-button type="text" size="small">调岗</el-button>
              <el-button type="text" size="small">离职</el-button>
              <el-button type="text" size="small" @click="showRole(scope.row.id)">角色</el-button>
              <el-button v-if="checkPermission('aa')" type="text" size="small" @click="delEmployee(scope.row.id)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
        <!-- 分页组件 -->
        <el-row type="flex" justify="center" align="middle" style="height: 60px">
          <el-pagination
            layout="prev, pager, next"
            :total="total"
            :page-size="pageSetting.size"
            @current-change="currentChange"
          />
        </el-row>
      </el-card>
      <!-- 引入弹出层组件 -->
      <addEmployee :show-dialog="showDialog" />
      <!-- 导入角色弹出层 -->
      <AssignRole ref="AssignRole" :is-show-dialog="isShowRole" />

    </div>
  </div>
</template>

<script>
import { getEmployee, delEmployee } from '@/api/employees.js'
import { export_json_to_excel } from '@/utils/export2Excel'
import addEmployee from './component/add-employee.vue'
import AssignRole from './component/assign-role.vue'
// import Blob from '@/utils/Blob.js'
export default {
  components: {
    addEmployee,
    AssignRole
  },
  data() {
    return {
      pageSetting: {
        page: 1,
        size: 5
      },
      employeeList: [],
      total: 0,
      showDialog: false,
      isShowRole: false
    }
  },
  created() {
    this.loadPage()
  },
  methods: {
    async showRole(id) {
      await this.$refs.AssignRole.getUserDetail(id)

      this.isShowRole = true
    },
    formatJson(filterVal, jsonData) {
      return jsonData.map(v => filterVal.map(j => v[j]))
    },
    async loadPage() {
      const res = await getEmployee(this.pageSetting)
      console.log(res)
      this.employeeList = res.data.rows.map(item => {
        return { ...item, enableState: item.enableState || 1 }
      })
      this.total = res.data.total
    },
    currentChange(newPage) {
      this.pageSetting.page = newPage
      this.loadPage()
    },
    formatEmployment(row, col, cell, index) {
      const hireType = [
        { id: 1, value: '正式工' },
        { id: 2, value: '临时工' }
      ]
      const obj = hireType.find(item => item.id === cell)
      return obj ? obj.value : '未知形式'
    },
    async exportToExcel() {
      // console.log('导出按钮执行')
      // export_json_to_excel({
      //   header: ['姓名', '年龄'],
      //   data: [
      //     ['tom', 12],
      //     ['jerry', 12]
      //   ]
      // })

      const res1 = await getEmployee({ page: 1, size: this.total })
      console.log('这是所有员工信息', res1.data.rows)
      const dict = {
        '入职日期': 'timeOfEntry',
        '手机号': 'mobile',
        '姓名': 'username',
        '转正日期': 'correctionTime',
        '工号': 'workNumber'
      }
      const header = Object.keys(dict)
      console.log(header)
      const data = res1.data.rows.map(user => {
        const userArr = []
        for (const key in dict) {
          const newKey = dict[key]
          userArr.push(user[newKey])
        }
        return userArr
      })
      console.log(data)
      // const data1 = this.formatJson(Object.values(dict), data)
      export_json_to_excel({ header, data, sssss: 'ss' })
    },
    async delEmployee(id) {
      await this.$confirm('是否确认刪除？')
      await delEmployee(id)
      this.$message.success('刪除成功')
      this.loadPage()
    }
  }

}
</script>

<style>

</style>
